package com.ruoyi.web.controller.weiriya;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.weiriya.domain.WLeaveWord;
import com.ruoyi.weiriya.service.IWLeaveWordService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 用户反馈Controller
 * 
 * @author lynn
 * @date 2023-08-01
 */
@Api(tags = "用户反馈")
@RestController
@RequestMapping("/weiriya/word")
public class WLeaveWordController extends BaseController
{
    @Autowired
    private IWLeaveWordService wLeaveWordService;

    /**
     * 查询用户反馈列表
     */
    @ApiOperation("获取用户反馈列表")
    @PreAuthorize("@ss.hasPermi('weiriya:word:list')")
    @GetMapping("/list")
    public TableDataInfo list(WLeaveWord wLeaveWord)
    {
        startPage();
        List<WLeaveWord> list = wLeaveWordService.getList(wLeaveWord);
        return getDataTable(list);
    }

    /**
     * 导出用户反馈列表
     */
    @ApiOperation("导出用户反馈列表")
    @PreAuthorize("@ss.hasPermi('weiriya:word:export')")
    @Log(title = "用户反馈", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, WLeaveWord wLeaveWord)
    {
        List<WLeaveWord> list = wLeaveWordService.getList(wLeaveWord);
        ExcelUtil<WLeaveWord> util = new ExcelUtil<WLeaveWord>(WLeaveWord.class);
        util.exportExcel(response, list, "用户反馈数据");
    }

    /**
     * 导入用户反馈列表
     */
    @ApiOperation("导入用户反馈列表")
    @PreAuthorize("@ss.hasPermi('weiriya:word:import')")
    @Log(title = "用户反馈", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file) throws Exception
    {
        ExcelUtil<WLeaveWord> util = new ExcelUtil<WLeaveWord>(WLeaveWord.class);
        List<WLeaveWord> list = util.importExcel(file.getInputStream());
        return success(wLeaveWordService.saveBatch(list));
    }

    /**
     * 获取用户反馈详细信息
     */
    @ApiOperation("获取用户反馈详细")
    @ApiImplicitParam(name = "id", value = "用户反馈ID", required = true, dataType = "String", paramType = "path")
    @PreAuthorize("@ss.hasPermi('weiriya:word:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        WLeaveWord byId = wLeaveWordService.getWLeaveWordById(id);

        String openId = SecurityUtils.getLoginUser().getOpenId();
        // 修改消息状态
        if(byId.getState() == 1 && StringUtils.isNotBlank(openId)){
            byId.setState(2);
        }
        if(byId.getState() == 3 && StringUtils.isBlank(openId)){
            byId.setState(4);
        }
        wLeaveWordService.updateById(byId);

        return success(byId);
    }

    /**
     * 新增用户反馈
     */
    @ApiOperation("新增用户反馈")
    @PreAuthorize("@ss.hasPermi('weiriya:word:add')")
    @Log(title = "用户反馈", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody WLeaveWord wLeaveWord)
    {
        wLeaveWord.setState(3);
        wLeaveWord.setMemberId(SecurityUtils.getUserId().toString());
        return toAjax(wLeaveWordService.save(wLeaveWord));
    }

    /**
     * 回复用户反馈
     */
    @ApiOperation("回复用户反馈")
    @PreAuthorize("@ss.hasPermi('weiriya:word:edit')")
    @Log(title = "用户反馈", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody WLeaveWord wLeaveWord)
    {
        return toAjax(wLeaveWordService.updateWLeaveWordById(wLeaveWord));
    }

    /**
     * 删除用户反馈
     */
    @ApiOperation("删除用户反馈")
    @ApiImplicitParam(name = "ids", value = "用户反馈ID集合", required = true, dataType = "String", paramType = "path")
    @PreAuthorize("@ss.hasPermi('weiriya:word:remove')")
    @Log(title = "用户反馈", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable List<String> ids)
    {
        return toAjax(wLeaveWordService.removeByIds(ids));
    }
}
